package com.ruoyi.mobile.api;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qsng.domain.*;
import com.ruoyi.qsng.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 积分商城
 *
 * @author gk
 * @date 2023-06-16
 */
@Api(tags = "积分商城")
@RestController
@RequestMapping
public class ShoppingController {

    @Autowired
    private IKShoppingMallService kShoppingMallService;

    @Autowired
    private IKMyExchangeService kMyExchangeService;

    @Autowired
    private IKCommodityTypeService kCommodityTypeService;

    @Autowired
    private IKShopBannerService kShopBannerService;

    @Autowired
    private IKUsersService kUsersService;

    @ApiOperation(value = "查询积分商城列表")
    @GetMapping("/weixin/shopping/list")
    public AjaxResults<List<KShoppingMall>> list(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,KShoppingMall kShoppingMall)
    {
        PageHelper.startPage(pageNum, pageSize);
        List<KShoppingMall> list = kShoppingMallService.selectKShoppingMallList(kShoppingMall);
        return AjaxResults.success(new PageSerializable<>(list));
    }

    @ApiOperation(value = "查询积分商城详情")
    @GetMapping("/weixin/shopping/{id}")
    public AjaxResults selectId(@PathVariable("id") String id)
    {

        return AjaxResults.success(kShoppingMallService.selectKShoppingMallById(id));
    }

    @ApiOperation(value = "兑换商品")
    @PostMapping("/weixin/exchange/add")
    public AjaxResults exchangeAdd(KMyExchange kMyExchange)
    {
        return kMyExchangeService.insertKMyExchangeForApp(kMyExchange);
    }

    @ApiOperation(value = "查询我兑换的商品")
    @GetMapping("/weixin/exchange/list")
    public AjaxResults<List<KMyExchange>>  exchangeList(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,KMyExchange kMyExchange)
    {
        PageHelper.startPage(pageNum, pageSize);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        kMyExchange.setUserId(String.valueOf(userId));
        return AjaxResults.success(new PageSerializable<>(kMyExchangeService.selectKMyExchangeList(kMyExchange)));
    }


    @ApiOperation(value = "商品核销")
    @GetMapping("/weixin/writeOff/{id}")
    public AjaxResults  writeOffId(@PathVariable("id") String id)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        KUsers kUsers=kUsersService.selectKUsersById(String.valueOf(userId));
        if(kUsers.getIsAdmin()==0){
            return  AjaxResults.error("您还不是管理员！");
        }
        return kMyExchangeService.updateStatusById(id);
    }


    @ApiOperation(value = "查询积分分类列表")
    @GetMapping("/weixin/getCommodityTypeList")
    public AjaxResults<List<KShoppingMall>> getCommodityTypeList(KCommodityType kCommodityType)
    {
        List<KCommodityType> list = kCommodityTypeService.selectKCommodityTypeList(kCommodityType);
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "查询商城轮播图")
    @GetMapping("/weixin/selectKShopBannerList")
    public AjaxResults selectKShopBannerList(KShopBanner kShopBanner)
    {
        List<KShopBanner> list = kShopBannerService.selectKShopBannerList(kShopBanner);
        return AjaxResults.success(list);
    }
}
